package view.frontend.order;

import exceptionManager.ExceptionManager;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.text.*;
import view.generaldesign.DesignLayout;

/*
 @author Mathias and Sonny Sandberg
 */
public class OrderInscription {

    private JPanel inscriptionPanel;
    private JTextField
            tFieldFont,
            tFieldNameAmount;
    private JTextPane 
            tPaneInscrption,
            tPaneCurrentNames;
    private JCheckBox cBoxRepaint;
    private Order order;

    public OrderInscription(Order view) {
        order = view;

        inscriptionPanel = new JPanel(null);
        inscriptionPanel.setBounds(0, 0, 300, 460);
        inscriptionPanel.setOpaque(false);
        inscriptionPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Inskription", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));

        JLabel lblFont = new JLabel("Skrifttype");
        lblFont.setBounds(10, 25, 280, 18);
        lblFont.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));

        JLabel lblNameAmount = new JLabel("Antal Navne:");
        lblNameAmount.setBounds(10, 105, 260, 18);
        lblNameAmount.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));

        tFieldFont = new JTextField();
        tFieldFont.setBounds(10, 45, 280, 25);
        tFieldFont.setBorder(DesignLayout.T_FIELD_RED);
        tFieldFont.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldFont.setFont(DesignLayout.STANDARD);
        tFieldFont.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                order.getOrderObject().getTombStone().setFont(tFieldFont.getText());
            }
        });

        tFieldNameAmount = new JTextField("0");
        tFieldNameAmount.setBounds(265, 105, 20, 25);
        tFieldNameAmount.setBorder(DesignLayout.T_FIELD_RED);
        tFieldNameAmount.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldNameAmount.setFont(DesignLayout.STANDARD);
        tFieldNameAmount.setHorizontalAlignment(JTextField.CENTER);
        tFieldNameAmount.addFocusListener(new FocusListener() 
        {
            @Override
            public void focusGained(FocusEvent e)
            {
                tFieldNameAmount.selectAll();
            }

            @Override
            public void focusLost(FocusEvent e)
            {
                tFieldNameAmount.select(0, 0);
            }
        });
        tFieldNameAmount.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                try
                {
                    order.getOrderObject().getTombStone().setNumNamesRoomFor(Integer.parseInt(tFieldNameAmount.getText()));
                } 
                catch (NumberFormatException evt)
                {
                    tFieldNameAmount.setText(""+order.getOrderObject().getTombStone().getNumNamesRoomFor());
                }                
            }
        });
        
        cBoxRepaint = new JCheckBox("Opmaling/Oppudsning?");
        cBoxRepaint.setBounds(10, 75, 280, 20);
        cBoxRepaint.setOpaque(false);
        cBoxRepaint.setFont(DesignLayout.STANDARD);
        cBoxRepaint.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                order.getOrderObject().getTombStone().setRepaint(cBoxRepaint.isSelected());
            }
        });
        
        // Current Names Start
        JLabel lblCurrentNames = new JLabel("Eksisterende Navne", JLabel.CENTER);
        lblCurrentNames.setBounds(10, 140, 280, 18);
        lblCurrentNames.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));

        tPaneCurrentNames = new JTextPane();
        tPaneCurrentNames.setFont(DesignLayout.STANDARD);
        StyledDocument doc2 = tPaneCurrentNames.getStyledDocument();
        SimpleAttributeSet center2 = new SimpleAttributeSet();
        StyleConstants.setAlignment(center2, StyleConstants.ALIGN_CENTER);
        doc2.setParagraphAttributes(0, doc2.getLength(), center2, false);
        tPaneCurrentNames.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                String[] lines = tPaneCurrentNames.getText().split("\\r?\\n");
                order.getOrderObject().getTombStone().getOldName().clear();
                for (String value : lines)
                {
                    order.getOrderObject().getTombStone().addToOldName(value);
                }
            }
        });
        
        JScrollPane currentNamesScroll = new JScrollPane();
        currentNamesScroll.setBorder(DesignLayout.T_FIELD_RED);
        currentNamesScroll.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        currentNamesScroll.setBounds(10, 160, 280, 80);
        currentNamesScroll.setViewportView(tPaneCurrentNames);
        currentNamesScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        currentNamesScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        currentNamesScroll.getVerticalScrollBar().setUnitIncrement(15);
        // Current Names End  
        
        // Inscription Start
        JLabel lblNewName = new JLabel("Inskription", JLabel.CENTER);
        lblNewName.setBounds(10, 245, 280, 18);
        lblNewName.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));

        tPaneInscrption = new JTextPane();
        tPaneInscrption.setFont(DesignLayout.STANDARD);
        StyledDocument doc = tPaneInscrption.getStyledDocument();
        SimpleAttributeSet center = new SimpleAttributeSet();
        StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
        doc.setParagraphAttributes(0, doc.getLength(), center, false);
        tPaneInscrption.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                String[] lines = tPaneInscrption.getText().split("\\r?\\n");
                String inscription = "";
                
                order.getOrderObject().getTombStone().getInscription().clear();
                for (int i = 0; i < lines.length; i++)
                {
                    if (i == lines.length-1)
                    {
                        order.getOrderObject().getTombStone().addToInscription(lines[i]);
                    }
                    else
                    {
                        order.getOrderObject().getTombStone().addToInscription(lines[i]+"\n\r");
                    }
                    inscription += lines[i];
                }
                inscription = inscription.replaceAll("\\s", "");                                    // Mellemrum skal ikke tælles med
                inscription = inscription.replace(".", "");                                         // Punktum er gratis
                order.getOrderObject().getTombStone().setNameCharacterCount(inscription.length());
            }
        });
        
        JScrollPane inscriptionScroll = new JScrollPane();
        inscriptionScroll.setBorder(DesignLayout.T_FIELD_RED);
        inscriptionScroll.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        inscriptionScroll.setBounds(10, 265, 280, 180);
        inscriptionScroll.setViewportView(tPaneInscrption);
        inscriptionScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        inscriptionScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        inscriptionScroll.getVerticalScrollBar().setUnitIncrement(15);
        // Inscription End        
        
        inscriptionPanel.add(lblFont);
        inscriptionPanel.add(lblNameAmount);
        inscriptionPanel.add(tFieldFont);
        inscriptionPanel.add(cBoxRepaint);
        inscriptionPanel.add(tFieldNameAmount);
        inscriptionPanel.add(lblCurrentNames);
        inscriptionPanel.add(lblNewName);
        inscriptionPanel.add(inscriptionScroll);
        inscriptionPanel.add(currentNamesScroll);

        inscriptionPanel.setVisible(false);
    }
    
    public void setFieldsFromModel()
    {
        model.TombStone tombObject = order.getOrderObject().getTombStone();
        tFieldFont.setText(tombObject.getFont());
        cBoxRepaint.setSelected(tombObject.isRepaint());
        tFieldNameAmount.setText(""+tombObject.getNumNamesRoomFor());
        
        StyledDocument nameDoc = tPaneCurrentNames.getStyledDocument();
        try
        {
            for (int i = 0; i < tombObject.getOldName().size(); i++)
            {
                nameDoc.insertString(nameDoc.getLength(), tombObject.getOldName().get(i), null);
            }            
        } 
        catch (BadLocationException ex)
        {
            ExceptionManager.handleBadLocationException(ex);
        }
        
        StyledDocument insDoc = tPaneInscrption.getStyledDocument();
        try
        {
            for (int i = 0; i < tombObject.getInscription().size(); i++)
            {
                insDoc.insertString(insDoc.getLength(), tombObject.getInscription().get(i), null);
            }            
        } 
        catch (BadLocationException ex)
        {
            ExceptionManager.handleBadLocationException(ex);
        }
    }

    public JPanel getPanel() {
        return inscriptionPanel;
    }
}